/**
 * Copyright 2018 orivil.com. All rights reserved.
 * Use of this source code is governed by a MIT-style
 * license that can be found at https://mit-license.org.
 */

package middle

import (
	"gitee.com/tugoer/orivil/mux"
	"gitee.com/tugoer/psy-testing/code/actions"
	"gitee.com/tugoer/psy-testing/code/api/tags"
)

var Admin mux.BeforeHandler = func(ctx *mux.Context) {
	a := actions.GetAdminFromSession(ctx)
	if a == nil {
		ctx.MsgWarning("管理员未登陆", "")
	}
}

func Set() {
	mux.SetBefore(Admin, func(act *mux.Action) bool {
		if act.ContainsTags(tags.Advertisement) && act.Method != "GET" ||
			act.ContainsTags(tags.Spoof) && act.Method != "GET" {
			return true
		}
		if act.ContainsTags(tags.Admin) &&
			act.Route != actions.RouteAdminLogin &&
			act.Route != actions.RouteAdminInfo {
			return true
		}
		return false
	})
}
